package cn.itcast.p2p20.fragments;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.UUID;

import cn.itcast.p2p20.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class SimpleFragment extends Fragment {


    private String title;

    public static SimpleFragment createSimpleFragment(String title) {

        SimpleFragment simpleFragment = new SimpleFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        simpleFragment.setArguments(args);
        return simpleFragment;
    }

    @Override
    public void setArguments(Bundle args) {
        title = args.getString("title", "");
        super.setArguments(args);
    }

    @Override
    public void onAttach(Context context) {
        Log.d(title, "onAttach() called");
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Log.d(title, "onCreate() called");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.d(title, "onCreateView() called");
        return inflater.inflate(R.layout.fragment_simple, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String title = getArguments().getString("title", "");
        ((TextView) view.findViewById(R.id.textview)).setText(title);
        String string = UUID.randomUUID().toString();
        ((ListView) view.findViewById(R.id.listview)).setAdapter(new ArrayAdapter<String>(view.getContext(),
                android.R.layout.simple_list_item_1, android.R.id.text1,string.split("\\B")));
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(title, "onActivityCreated() called");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(title, "onStart() called");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(title, "onResume() called");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(title, "onPause() called");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(title, "onStop() called");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(title, "onDestroyView() called");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(title, "onDestroy() called");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(title, "onDetach() called");
    }
}
